package com.base.utils

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.utils.DensityUtils
import java.io.ByteArrayOutputStream
import java.io.File


class LoadImageUtils {
    companion object {
        /**
         * 选中图片切换
         */
        fun setCheckedSrc(
            view: ImageView,
            checked: Boolean,
            checkedResId: Drawable?,
            uncheckedResId: Drawable?
        ) {
            if (checked) view.setImageDrawable(checkedResId) else view.setImageDrawable(
                uncheckedResId
            )
        }

        /**
         * 加载本地图片
         *
         * @param imageView
         * @param file
         */
        fun loadImage(imageView: ImageView, file: File?) {
            loadImage(imageView, file, 0f)
        }


        /**
         * 加载本地图片 带圆角
         *
         * @param imageView
         * @param file
         * @param radius
         */
        fun loadImage(imageView: ImageView, file: File?, radius: Float) {
            val requestOptions: RequestOptions = if (radius == 0f) {
                RequestOptions()
            } else {
                RequestOptions.bitmapTransform(
                    RoundedCorners(
                        DensityUtils.dp2px(
                            imageView.context,
                            radius
                        )
                    )
                )
            }
            Glide.with(imageView.context).load(file).apply(requestOptions).into(imageView)
        }

        /**
         * 加载bimap
         *
         * @param imageView
         * @param bitmap
         */
        fun loadImage(imageView: ImageView, bitmap: Bitmap) {
            loadImage(imageView, bitmap, 0f)
        }


        /**
         * 加载bimap圆角
         *
         * @param imageView
         * @param bitmap
         * @param radius
         */
        fun loadImage(imageView: ImageView, bitmap: Bitmap, radius: Float) {
            val baos = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)
            val bytes = baos.toByteArray()
            val requestOptions: RequestOptions = if (radius == 0f) {
                RequestOptions()
            } else {
                val roundedCorners = RoundedCorners(DensityUtils.dp2px(imageView.context, radius))
                RequestOptions.bitmapTransform(roundedCorners)
            }
            Glide.with(imageView.context).load(bytes).apply(requestOptions).into(imageView)
        }

        /**
         * 加载resId
         *
         * @param imageView
         * @param resId
         */
        fun loadImage(imageView: ImageView, resId: Int) {
            loadImage(imageView, resId, 0f)
        }

        /**
         * 加载resId
         *
         * @param imageView
         * @param resId
         * @param radius
         */
        fun loadImage(imageView: ImageView, resId: Int, radius: Float) {
            val requestOptions: RequestOptions = if (radius == 0f) {
                RequestOptions()
            } else {
                val roundedCorners = RoundedCorners(DensityUtils.dp2px(imageView.context, radius))
                RequestOptions.bitmapTransform(roundedCorners)
            }
            Glide.with(imageView.context).load(resId).apply(requestOptions).into(imageView)
        }

        /**
         * 加载网络图片
         *
         * @param imageView
         * @param url
         */
        fun loadImage(imageView: ImageView, url: String?) {
            loadImage(imageView, url, null, null, 0f)
        }


        /**
         * 加载网络图片带圆角
         *
         * @param imageView
         * @param url
         * @param radius
         */
        fun loadImage(imageView: ImageView, url: String?, radius: Float) {
            loadImage(imageView, url, null, null, radius)
        }


        /**
         * 加载网络图片
         *
         * @param imageView
         * @param url
         * @param drawableHolder 加载中图片
         * @param errorDrawable  加载错误图片
         */
        fun loadImage(
            imageView: ImageView,
            url: String?,
            drawableHolder: Drawable,
            errorDrawable: Drawable
        ) {
            loadImage(imageView, url, drawableHolder, errorDrawable, 0f)
        }

        /**
         * 加载网络图片
         *
         * @param imageView
         * @param url
         * @param drawableHolderId 加载中图片
         * @param errorDrawableId  加载错误图片
         */
        fun loadImage(
            imageView: ImageView,
            url: String?,
            drawableHolderId: Int?,
            errorDrawableId: Int?
        ) {
            var drawableHolder: Drawable? = drawableHolderId?.let {
                imageView.context.getDrawable(it)
            }
            var errorDrawable: Drawable? = errorDrawableId?.let {
                imageView.context.getDrawable(it)
            }
            loadImage(imageView, url, drawableHolder, errorDrawable, 0f)
        }

        /**
         * 加载网络图片带等待图片错误图片
         *
         * @param imageView
         * @param url
         * @param drawableHolder 加载中图片
         * @param errorDrawable  加载错误图片
         * @param radius         圆角
         */
        fun loadImage(
            imageView: ImageView,
            url: String?,
            drawableHolder: Drawable?,
            errorDrawable: Drawable?,
            radius: Float
        ) {
            val requestOptions: RequestOptions = if (radius == 0f) {
                RequestOptions()
            } else {
                RequestOptions.bitmapTransform(
                    RoundedCorners(
                        DensityUtils.dp2px(
                            imageView.context,
                            radius
                        )
                    )
                )
            }
            drawableHolder?.let {
                requestOptions.placeholder(drawableHolder)
            }
            errorDrawable?.let {
                requestOptions.error(errorDrawable)
            }
            val glide = Glide.with(imageView.context)
                .load(url)
                .apply(requestOptions)
            glide.into(imageView)

        }

        /**
         * 加载本地图片 圆形
         *
         * @param imageView
         * @param file
         * @param radius
         */
        fun loadImageCircleCrop(imageView: ImageView, file: File?) {
            val requestOptions = RequestOptions.circleCropTransform()
            Glide.with(imageView.context).load(file).apply(requestOptions).into(imageView)
        }

        /**
         * 加载网络图片带等待图片错误图片
         *
         * @param imageView
         * @param url
         */
        fun loadImageCircleCrop(
            imageView: ImageView,
            url: String?
        ) {
            val requestOptions = RequestOptions.circleCropTransform()
            Glide.with(imageView.context)
                .load(url)
                .apply(requestOptions).into(imageView)

        }
    }
}